package com.six.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.core.entity.Permission;
import com.six.core.entity.Role;
import com.six.core.entity.RolePermission;
import com.six.core.mapper.PermissionMapper;
import com.six.core.mapper.RoleMapper;
import com.six.core.service.RolePermissionService;
import com.six.core.service.RoleService;
import com.six.core.vo.EmpRoleVo;
import com.six.core.vo.Result;
import com.six.core.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.java2d.pipe.AAShapePipe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author:LiWei
 * @date:2023年02月27日18:53
 */
@Service
public class RoleServiceImpl  implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionService rolePermissionService;

    //根据角色名查询
    @Override
    public Result findByRoleName(Integer currentPage, Integer pageSize, String roleName) {
        Page page=new Page(currentPage,pageSize);
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(roleName!=null,"role_name",roleName);
        roleMapper.selectPage(page,queryWrapper);
        return new Result(200,"查询角色成功!",page);
    }

    //根据用户id查询具备权限的角色
    @Override
    public Result findByUserId(Integer roleId) {
        //查询所有角色
        QueryWrapper wrapper = new QueryWrapper();
        List list = roleMapper.selectMaps(wrapper);
        System.out.println("list = " + list);
        //根据用户id查询具有的角色id
        List<Integer> byUserId = roleMapper.findByUserId(roleId);

        System.out.println("byUserId = " + byUserId);
        //回显到map
        HashMap hashMap = new HashMap();
        hashMap.put("list",list);
        hashMap.put("userId",byUserId);
        return new Result(200,"查询成功",hashMap);

    }


    //分配角色
    @Override
    public Result fenRole(EmpRoleVo empRoleVo) {
        List<Integer> roleIds = empRoleVo.getRoleIds();
        Integer userId = empRoleVo.getUserId();
        for (Integer roleId : roleIds) {
            roleMapper.fenUserRole(roleId,userId);
        }
        System.out.println("roleIds: " + empRoleVo.getRoleIds());
        return new Result(200,"分配成功",null);
    }


    //删除角色
    @Override
    public Result deleteRoleById(Integer roleid) {
        int i = roleMapper.deleteById(roleid);
        if(i>0){
            return new Result(200,"删除成功!");
        }
        return new Result(500,"删除失败!");
    }

    @Override
    public Result findAll(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        Page<Role> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.like(roleVo.getRoleName()!=null,"role_name",roleVo.getRoleName());
        wrapper.like(roleVo.getIsDisable() != null,"is_disable",roleVo.getIsDisable());
        Page<Role> rolePage = roleMapper.selectPage(page, wrapper);
        return new Result(200,"查询成功",rolePage);


    }

    @Override
    public Result updateRole(Role role) {
        int i = roleMapper.updateById(role);
        if (i>0){
            return new Result(200,"修改成功!",null);
        }
        return new Result(500,"修改失败!",null);
    }

    @Override
    public Result findPermissionByRoleId(Integer roleId) {

        //根据角色id查询所有权限
        //然后查询角色所具有的权限
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        List<Permission> permissionList = permissionMapper.selectList(wrapper);
        //一级菜单
        List<Permission> firsts = new ArrayList<>();
        for (Permission first : permissionList) {
            if (first.getPid().equals(0)) {


                firsts.add(first);
            }
        }
        //二级菜单
        for (Permission second : firsts) {
            second.setChildren(findChildren(permissionList,second.getId()));
        }

        QueryWrapper<RolePermission> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("role_id",roleId);
        List<RolePermission> list = rolePermissionService.list(wrapper1);
        //用stream只查出id
        List<Integer> collect = list.stream().map(iteam -> iteam.getPermissionId()).distinct().collect(Collectors.toList());

        //封装到map里
        Map<String,Object> map=new HashMap<>();
        map.put("checkIds",collect);
        map.put("treeData",firsts);

        return new Result(200,"查询成功!",map);
    }

    @Override
    public Result addRole(Role role) {
        int insert = roleMapper.insert(role);
        if (insert>0){
            return new Result(200,"添加成功",insert);
        }else {
            return new Result(500,"添加失败",insert);
        }
    }

    private List<Permission> findChildren(List<Permission> permissionList, Integer id){
        List<Permission> children = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getPid().equals(id)) {
                children.add(permission);
            }
        }
        for (Permission child : children) {
            child.setChildren(findChildren(permissionList,child.getId()));
        }
        return children;
    }
}









